package com.cz.ui.friend;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.fragment.app.Fragment;

import com.cz.R;
import com.cz.adapter.friend.FriendsListAdapter;
import com.cz.pojo.Friend;
import com.cz.utils.DBOpenHelper;

import java.util.ArrayList;

public class FriendList extends Fragment {

    private DBOpenHelper dbOpenHelper;
    private ListView chatList;
    private ArrayList<Friend> friends;
    private  FriendsListAdapter chatAdapter;
    private final String LABEL = "标签";
    private final String NEW_FRIEND = "新的朋友";
    private final String THE_PUBLIC = "公众号";
    private final String GROUP_CHAT = "群聊";


    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_friends_list, container, false);

        chatList = root.findViewById(R.id.listView_chat_record);

        return root;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        selectDatabase();
    }

    /**
     * 数据库查询
     */
    private void selectDatabase() {
        dbOpenHelper = new DBOpenHelper(getActivity(),"WeChatMini",null,2);
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();

        friends = new ArrayList<>();
        friends.add(new Friend("0", LABEL, "icon_frend_label"));
        friends.add(new Friend("0", NEW_FRIEND, "icon_friend_add"));
        friends.add(new Friend("0", THE_PUBLIC, "icon_friends_publc"));
        friends.add(new Friend("0", GROUP_CHAT, "icon_friends_all"));

        insertInfo1(db);
        insertInfo2(db);
        insertInfo3(db);
        insertInfo4(db);
        insertInfo5(db);
        insertInfo6(db);
        insertInfo7(db);
        insertInfo8(db);
        insertInfo9(db);
        insertInfo10(db);
        insertInfo11(db);
        insertInfo12(db);

        //满足条件查询
        Cursor user = db.rawQuery("select * from friends", null);
        while(user.moveToNext()) {
            //根据查出来的数据封装成好友对象
            String uid = user.getString(0);
            String nickname = user.getString(1);
            String avatar = user.getString(4);
            Friend friend = new Friend(uid, nickname, avatar);
            friends.add(friend);
        }
        chatAdapter = new FriendsListAdapter(getActivity(), friends);
        chatList.setAdapter(chatAdapter);
        //好友点击事件
        ListClickListener l = new ListClickListener();
        chatList.setOnItemClickListener(l);

        db.close();
    }

    //点击事件监听类
    class ListClickListener implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            //通过view获取组件中的nickName
            String nickName = ((TextView) view.findViewById(R.id.tv_user_name)).getText().toString();

            if(
                nickName.equals(LABEL) ||
                nickName.equals(NEW_FRIEND) ||
                nickName.equals(THE_PUBLIC) ||
                nickName.equals(GROUP_CHAT)){
                return;
            }
            //进行页面跳转
            Intent intent = new Intent(parent.getContext(), FriendInfo.class);
            intent.putExtra("nickName",nickName);
            startActivity(intent);
        }
    }


    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo1(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","1");
        contentValues.put("nickname","BiShuGui");
        contentValues.put("remark","毕术桂");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","bisugui");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }


    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo2(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","2");
        contentValues.put("nickname","ZhongShengJie");
        contentValues.put("remark","钟胜杰");
        contentValues.put("phone","987456321123");
        contentValues.put("avatar","zhongshengjie");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo3(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","3");
        contentValues.put("nickname","ZhuJiaCheng");
        contentValues.put("remark","朱嘉城");
        contentValues.put("phone","9835787654357");
        contentValues.put("avatar","zhujiacheng");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo4(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","4");
        contentValues.put("nickname","ChenZhuo");
        contentValues.put("remark","陈卓");
        contentValues.put("phone","15310173757");
        contentValues.put("avatar","icon");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo5(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","5");
        contentValues.put("nickname","孤单摩天轮");
        contentValues.put("remark","杀马特");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","shamate");
        contentValues.put("address","社会大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo6(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","6");
        contentValues.put("nickname","寂寞非主流");
        contentValues.put("remark","非主流");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","feizhuliu");
        contentValues.put("address","社会大学");
        long mine = db.insert("friends",null, contentValues);
    }


    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo7(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","7");
        contentValues.put("nickname","测试名字0");
        contentValues.put("remark","毕术桂");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","avatar");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }


    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo8(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","8");
        contentValues.put("nickname","测试名字1");
        contentValues.put("remark","钟胜杰");
        contentValues.put("phone","987456321123");
        contentValues.put("avatar","zhongshengjie");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo9(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","9");
        contentValues.put("nickname","测试名字2");
        contentValues.put("remark","朱嘉城");
        contentValues.put("phone","9835787654357");
        contentValues.put("avatar","avatar");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo10(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","10");
        contentValues.put("nickname","测试名字3");
        contentValues.put("remark","陈卓");
        contentValues.put("phone","15310173757");
        contentValues.put("avatar","icon");
        contentValues.put("address","重庆理工大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo11(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","11");
        contentValues.put("nickname","测试名字4");
        contentValues.put("remark","杀马特");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","avatar");
        contentValues.put("address","社会大学");
        long mine = db.insert("friends",null, contentValues);
    }
    //用于数据持久化(插入方法只执行一次 ,多次执行将会报错,执行一次成功后请删除此方法)
    private void insertInfo12(SQLiteDatabase db) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("uid","12");
        contentValues.put("nickname","测试名字5");
        contentValues.put("remark","非主流");
        contentValues.put("phone","12345678920");
        contentValues.put("avatar","avatar");
        contentValues.put("address","社会大学");
        long mine = db.insert("friends",null, contentValues);
    }

    //数据删除
    private void delete(SQLiteDatabase db,String listid) {
        int dialoguelist = db.delete("friends", "uid=?", new String[]{listid});
        System.out.println(dialoguelist);
    }

}